package com.kly.cashmall.popup;

import android.app.Activity;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.kly.cashmall.R;
import com.kly.cashmall.bean.PermissionBean;
import com.kly.cashmall.module.products.adapter.ApplyForPermissionAdapter;
import com.kly.cashmall.module.products.adapter.ApplyForPermissionSimpleAdapter;
import com.kly.cashmall.module.web.WebViewActivity;
import com.kly.cashmall.utils.data.CollectionUtils;
import com.kly.cashmall.utils.function.ToastUtil;
import com.kly.cashmall.widget.delay.DelayClickTextView;

import razerdp.basepopup.BasePopupWindow;


/**
 * author :
 * date   : 2019-12-19 15:54
 * desc   :
 */
public class ApplyPermissionPopup extends BasePopupWindow implements View.OnClickListener {
    private final Callback callback;
    private DelayClickTextView popCancel;
    private DelayClickTextView popAgree;
    private Activity activity;
    private TextView popTip;
    private RecyclerView permissionList;
    private TextView popTermsTxt;
    private CheckBox popCheckTerms;

    public ApplyPermissionPopup(Activity activity, Callback callback) {
        super(activity);
        setContentView(R.layout.popup_apply_for_permission);
        this.callback = callback;
        this.activity = activity;
        showPopWindow();
    }

    private void showPopWindow() {
        initView();
        initEvent();
    }

    private void initView() {
        popTip = findViewById(R.id.pop_tip);
        popTermsTxt = findViewById(R.id.terms_txt);
        permissionList = findViewById(R.id.permission_list);
        popCheckTerms = findViewById(R.id.check_terms);
        popCancel = findViewById(R.id.pop_cancel);
        popAgree = findViewById(R.id.pop_agree);
    }

    private void initEvent() {
        popCancel.setOnClickListener(this);
        popAgree.setOnClickListener(this);
    }

    public void setData(PermissionBean data, String registerProtocol, String privacyPolicy) {
        permissionList.setLayoutManager(new LinearLayoutManager(getContext()));
        popTip.setText(data.getTips());
        if (!CollectionUtils.isEmpty(data.getPermissionList())){
            ApplyForPermissionAdapter adapter = new ApplyForPermissionAdapter();
            permissionList.setAdapter(adapter);
            adapter.setList(data.getPermissionList());
        }else {
            ApplyForPermissionSimpleAdapter adapter = new ApplyForPermissionSimpleAdapter();
            permissionList.setAdapter(adapter);
            adapter.setList(data.getPermissions());
        }
        setPrivacyText(registerProtocol,privacyPolicy);
    }
    public void setPrivacyText(String registerUrl, String privacyUrl){
        String registerAgreement = activity.getString(R.string.all_load_agreement);
        popTermsTxt.setText(registerAgreement);
        String termsConditions =  activity.getString(R.string.login_terms_conditions);
        String privacyPolicy =  activity.getString(R.string.privacy_policy);
        int tcStart = registerAgreement.indexOf(termsConditions);
        int tcEnd = tcStart + termsConditions.length();

        int ppStart = registerAgreement.indexOf(privacyPolicy);
        int ppEnd = ppStart + privacyPolicy.length();

        SpannableString spannable = new SpannableString(registerAgreement);

        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View view) {
                activity.startActivity(WebViewActivity.callIntent(activity,activity.getString(R.string.login_terms_conditions),
                        registerUrl));
            }
        }, tcStart, tcEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        spannable.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                activity.startActivity(WebViewActivity.callIntent(activity,activity.getString(R.string.privacy_policy),
                        privacyUrl));
            }
        }, ppStart, ppEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

        popTermsTxt.setLinkTextColor(ContextCompat.getColor(activity, R.color.color_FD4C5C));
        popTermsTxt.setText(spannable);
        popTermsTxt.setMovementMethod(LinkMovementMethod.getInstance());
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.pop_cancel:
                callback.onCancelCallback();
                dismiss();
                break;
            case R.id.pop_agree:
                if (!popCheckTerms.isChecked()){
                    ToastUtil.showAppToast(activity.getString(R.string.loan_check_tip));
                    return;
                }
                callback.onCallback();
                dismiss();
                break;
            default:
                break;
        }

    }

    public interface Callback {
        void onCancelCallback();
        void onCallback();
    }
}